package models

import (
	"blog/pkg/setting"
	"log"
	"time"

	"github.com/glebarez/sqlite"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Model struct {
	Id
	CreatedBy string    `json:"created_by"`
	CreatedDt time.Time `json:"created_dt"`
	UpdateBy  string    `json:"update_by"`
	UpdateDt  time.Time `json:"update_dt"`
	IsDelete
	State
}

type Id struct {
	ID int64 `json:"id"`
}

type IsDelete struct {
	IsDelete bool `json:"is_delete"`
}

type State struct {
	State int32 `json:"state"`
}

type UserId struct {
	UserId int64 `json:"user_id"`
}

var sqlDb *gorm.DB

func init() {
	var err error

	if setting.SqlType == "mysql" {
		sqlDb, err = gorm.Open(mysql.Open(setting.Dns), &gorm.Config{})
	} else if setting.SqlType == "sqlite" {
		sqlDb, err = gorm.Open(sqlite.Open("/doc/gorm.db"), &gorm.Config{})
	}

	if err != nil {
		log.Panicf("链接数据库失败：err=%v", err)
	}

	// 得到通用数据库句柄
	db, err := sqlDb.DB()
	db.SetMaxIdleConns(20)
	db.SetMaxOpenConns(100)
	sqlDb.SingularTable(true)
}
